#include "stdafx.h"

#ifdef _UNIX
#include <dirent.h>
#endif

#ifdef _MSC_VER
#pragma warning(disable:4996)
#include <Windows.h>
#endif

#include <fstream>
#ifdef _USE_XEVOL3D_NEW_
#include "../mem/mem_operator.h"
#endif

#include "LaborFileSystem.h"
//#include "../BaseLib/xI18N.h"
NAMESPACE_LABOR3D_BEGIN


//IMPL_REFCOUNT_OBJECT_FUNCTION(xFileStream);

static FILE* xOpenFileRWB(const wchar_t* wcsFileName)
{
#ifdef _WIN32
	FILE* fp = _wfopen(wcsFileName , L"wb+");
#else
	xint8 mbsFileName[512]={0};
	XEvol_UnicodeToFsEnc(wcsFileName,mbsFileName,512);
	FILE* fp = fopen(mbsFileName , "wb+");
#endif
	return fp;
}

static FILE* OpenFileRB(const wchar_t* wcsFileName)
{
#ifdef _WIN32
	FILE* fp = _wfopen(wcsFileName , L"rb");
#else
	xint8 mbsFileName[512]={0};
	XEvol_UnicodeToFsEnc(wcsFileName,mbsFileName,512);
	FILE* fp = fopen(mbsFileName , "rb");
#endif
	return fp;
}


xFileSystem* xFileSystem::singleton()
{
	static xFileSystem sys;
	return &sys;
}



std::ds_wstring xFileSystem::getFileName(const wchar_t* fullName)
{
	if(fullName == NULL)
		return L"";
	int idx = (int)wcslen(fullName);
	for(; idx >= 0 ; idx --)
	{
		if(fullName[idx] == '\\' || fullName[idx]=='/')
		{
			break;
		}
	}
	std::ds_wstring _ret = fullName + (idx + 1);   
	return _ret;
}

std::ds_wstring xFileSystem::getPathName(const wchar_t* fullName)
{
	if(fullName == NULL)
		return L"";
	std::ds_wstring _ret = fullName;   
	std::ds_wstring::size_type pos = _ret.rfind('\\');
	if(pos == std::ds_wstring::npos) 
	{
		pos = _ret.rfind('/');
	}
	if(pos == std::ds_wstring::npos )
	{
		return L"";
	}

	_ret.replace(pos ,wcslen(fullName), L"");
	return _ret + PATH_SPLITTER_STRING;
}

bool xFileSystem::fileExist(const wchar_t* wcsFileName)
{
	FILE* fp = OpenFileRB(wcsFileName);
	bool bRet = fp != NULL;
	if(fp) fclose(fp);
	return bRet;
}

int xFileSystem::loadFile( xint8_u*& fileBufer , const wchar_t* wcsFileName)
{
	FILE* fp = OpenFileRB(wcsFileName);
	if(fp == NULL)
	{
		fileBufer = NULL;
		return 0;
	}

	fseek(fp,0,SEEK_END);
	int fLen = ftell(fp);
	fileBufer = new  xint8_u[fLen + 2];
    fseek(fp,0,SEEK_SET);
	fread(fileBufer,1,fLen,fp);
	fileBufer[fLen] = 0;
	fileBufer[fLen + 1] = 0;
	fclose(fp);
	return fLen;
}

void xFileSystem::unloadFile( xint8_u*& fileBufer)
{
	delete [] fileBufer;
	fileBufer = NULL;
}
bool xFileSystem::loadFile(const wchar_t* fileName , std::ifstream& stream , std::ios::open_mode _mode)
{
#ifdef _WIN32
	stream.open(fileName , _mode);
#else
	xint8 mbsFileName[512]={0};
	XEvol_UnicodeToFsEnc(fileName,mbsFileName,512);
	stream.open(mbsFileName , (std::_Ios_Openmode)_mode);
#endif

	
	if(stream.fail())
		return false;
	return true;
}

bool xFileSystem::openFile(const wchar_t* fileName , std::fstream& stream , std::ios::open_mode _mode)
{
#ifdef _WIN32
	stream.open(fileName , _mode);
#else
	xint8 mbsFileName[512]={0};
	XEvol_UnicodeToFsEnc(fileName,mbsFileName,512);
	stream.open(mbsFileName , (std::_Ios_Openmode)_mode);
#endif
	if(stream.fail())
		return false;
	return true;
}

const wchar_t* xFileSystem::getFileExtName(const wchar_t* filename )
{
	static wchar_t name_ext[16]={0};
	name_ext[0] = 0;
	size_t  len = wcslen(filename);
    if(len == 0)
        return name_ext;
	for( size_t i = len-1 ; i > 0 ; i -- )
	{
		if(filename[i] == '.')
		{
			wcsncpy(name_ext,filename + i + 1,16);
			{
				for(size_t j=0;j< (len - i) ; j++)
				{
					if(name_ext[j] < 127)
					{
				        name_ext[j] = tolower(name_ext[j]);
					}	
				}
			}
			break ;
		}
	}
	return name_ext;
}

NAMESPACE_LABOR3D_END



